//
//  KLMyUploadViewController.m
//  KunLun
//
//  Created by Mac on 2017/11/29.
//  Copyright © 2017年 stoneobs@icloud.com. All rights reserved.
//

#import "KLMyUploadViewController.h"
#import "KLMyuploadTableViewCell.h"
#import "KLUploadModel.h"
@interface KLMyUploadViewController ()
@property(nonatomic, strong) NSMutableArray                     *dataSouce;
@end

@implementation KLMyUploadViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"我的上传";
    
    self.tableView.st_noreslutView = [[STNoresultView alloc] initWithFrame:CGRectMake(0, 100, UIScreenWidth, 400) title:@"暂无上传" buttonTitle:@"" buttonHandle:^(NSString *titleString) {
    
    }];
    
    self.tableView.mj_header  = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self sendMyUpLoadRequest];
    }];
    [self.tableView.mj_header beginRefreshing];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma --mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataSouce.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return [KLMyuploadTableViewCell cellHeight];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10;
}
- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}
- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}
- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString * reuseIdentifier =  @"cell";
    KLMyuploadTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseIdentifier];
    if (!cell) {
        cell = [[KLMyuploadTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:reuseIdentifier];
        
    }
    __weak typeof(self) weakSelf =  self;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell.contractButton setClicAction:^(UIButton *sender) {
        [weakSelf onSlectedContactModelWithIndexPath:indexPath];
    }];
    cell.model = self.dataSouce[indexPath.row];
    return cell;
}
#pragma --mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    KLUploadModel * mdoel = self.dataSouce[indexPath.row];
    KLGoodDetailViewController * vc = [KLGoodDetailViewController new];
    vc.goodID = mdoel.goods_id.description;
    [self.navigationController pushViewController:vc animated:YES];
    
    [self.tableView deselectRowAtIndexPath:indexPath animated:NO];
}
#pragma mark --Action Method
- (void)onSlectedContactModelWithIndexPath:(NSIndexPath*)indexPath{
    KLUploadModel * model = self.dataSouce[indexPath.row];
    NSString * phone = model.username ;
    BOOL isSelf =  [[STUserManger defult].phone isEqualToString:phone];
    if (isSelf) {
        [SVProgressHUD showInfoWithStatus:@"这是你自己发布的产品哟"];
        return;
    }
    if (!phone.length) {
        [SVProgressHUD showInfoWithStatus:@"暂无电话号码"];
        return;
    }
    KLMessageChatViewController * vc = [[KLMessageChatViewController alloc] initWithConversationChatter:phone conversationType:EMConversationTypeChat];
    [self.navigationController pushViewController:vc animated:YES];
}
#pragma mark --NetWork Method
- (void)sendMyUpLoadRequest{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * dic = [NSMutableDictionary new];
    NSString * url = [NSString stringWithFormat:@"%@%@%@",serviceUrl,@"/api/user/screenshotlist",[NSString stringWithFormat:@"?token=%@",[STUserManger defult].token]];
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:dic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 NSArray * dataArray =  responseObject[@"data"];
                                                 self.dataSouce = [NSMutableArray new];
                                                 for (NSDictionary * modelDic in dataArray) {
                                                     KLUploadModel * model = [KLUploadModel mj_objectWithKeyValues:modelDic];
                                                     model.goods = [KLGoodModel mj_objectWithKeyValues:modelDic[@"goods"]];
                                                     [self.dataSouce addObject:model];
                                                 }
                                                 [self.tableView reloadData];
                                                 
                                             } failure:^(NSString *stateCode, STError *error) {
                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD dismiss];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                             }];
}
@end
